package com.fudan.backend.service.impl;

import com.fudan.backend.dao.ActivityDao;
import com.fudan.backend.dao.OrderDao;
import com.fudan.backend.dao.StoreDao;
import com.fudan.backend.dao.StoreParticipateInActivityRequestDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Activity;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.pojo.StoreParticipateInActivityRequest;
import com.fudan.backend.pojo.statics.RequestStatus;
import com.fudan.backend.service.OrderService;
import com.fudan.backend.service.StoreParticipateInActivityRequestService;
import com.fudan.backend.util.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StoreParticipateInActivityRequestServiceImpl implements StoreParticipateInActivityRequestService {

    @Autowired
    StoreParticipateInActivityRequestDao storeParticipateInActivityRequestDao;

    @Autowired
    StoreDao storeDao;

    @Autowired
    Snowflake snowflake;

    @Autowired
    ActivityDao activityDao;

    @Autowired
    OrderService orderService;

    @Override
    public RetCode addStoreParticipateInActivityRequest(StoreParticipateInActivityRequest storeParticipateInActivityRequest, String userId) {

        RetCode retCode;
        String storeId = storeParticipateInActivityRequest.getStoreId();

        Date date = new Date();

        storeParticipateInActivityRequest.setStoreParticipateInActivityRequestId((snowflake.nextId()).toString());
        storeParticipateInActivityRequest.setInitiateTime(date);

        Store store = storeDao.selectStoreByStoreId(storeId);
        Double storeRegisteredCapital = store.getRegisteredCapital();
        String activityId = store.getActivityId();//当前商店正在参加的活动


        Activity activity = activityDao.selectActivityByActivityId(storeParticipateInActivityRequest.getActivityId());//准备参加的活动
        Double registeredCapital = activity.getRegisteredCapital();
        Double monthlySalesValue = activity.getMonthlySalesValue();
        Integer monthlySalesVolume = activity.getMonthlySalesVolume();
        Date startTime = activity.getStartTime();

        long month = 30L * 24L * 60L * 60L * 1000L;

        Double storeMonthlySalesValue = orderService.storeSalesValue(storeId, new Date(startTime.getTime() - month), startTime);
        int storeMonthlySalesVolume = orderService.storeSalesVolume(storeId, new Date(startTime.getTime() - month), startTime);

        List<StoreParticipateInActivityRequest> unprocessedRequests = storeParticipateInActivityRequestDao.selectAllUnprocessedStoreParticipateInActivityRequestByStoreId(storeId);

        if (!store.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }


        if (!(activityId.equals("0"))) {//若当前商店已经在某个活动中
            storeParticipateInActivityRequest.setProcessTime(date);
            storeParticipateInActivityRequest.setStatus(RequestStatus.NOT_APPROVED);
            storeParticipateInActivityRequest.setInfo("自动审核：当前该商店已经参加了某个活动，不允许同时参加两个活动");
            retCode = RetCode.STORE_IN_ACTIVITY;
        } else if (unprocessedRequests.size() != 0) {
            storeParticipateInActivityRequest.setProcessTime(date);
            storeParticipateInActivityRequest.setStatus(RequestStatus.NOT_APPROVED);
            storeParticipateInActivityRequest.setInfo("自动审核：当前该商店已经提交了一个参加活动申请且该申请未被处理");
            retCode = RetCode.ACTIVITY_REQUEST_NOT_PROCESSED;
        } else if (storeRegisteredCapital < registeredCapital) {
            storeParticipateInActivityRequest.setProcessTime(date);
            storeParticipateInActivityRequest.setStatus(RequestStatus.NOT_APPROVED);
            storeParticipateInActivityRequest.setInfo("自动审核：该商店注册资金不符合活动要求");
            retCode = RetCode.REGISTERED_CAPITAL_NOT_ENOUGH;
        } else if (storeMonthlySalesValue < monthlySalesValue) {
            storeParticipateInActivityRequest.setProcessTime(date);
            storeParticipateInActivityRequest.setStatus(RequestStatus.NOT_APPROVED);
            storeParticipateInActivityRequest.setInfo("自动审核：该商店上月销售额不符合活动要求");
            retCode = RetCode.MONTHLY_SALES_VALUE_NOT_ENOUGH;
        } else if (storeMonthlySalesVolume < monthlySalesVolume) {
            storeParticipateInActivityRequest.setProcessTime(date);
            storeParticipateInActivityRequest.setStatus(RequestStatus.NOT_APPROVED);
            storeParticipateInActivityRequest.setInfo("自动审核：该商店上月销售量不符合活动要求");
            retCode = RetCode.MONTHLY_SALES_VOLUME_NOT_ENOUGH;
        } else {
            storeParticipateInActivityRequest.setStatus(RequestStatus.APPLYING);
            retCode = RetCode.ACTIVITY_SUCCESS;
        }

        storeParticipateInActivityRequestDao.insertStoreParticipateInActivityRequest(storeParticipateInActivityRequest);
        return retCode;
    }

    @Override
    public StoreParticipateInActivityRequest getStoreParticipateInActivityRequestById(String storeParticipateInActivityRequestId) {
        StoreParticipateInActivityRequest storeParticipateInActivityRequest = storeParticipateInActivityRequestDao.selectStoreParticipateInActivityRequestById(storeParticipateInActivityRequestId);
        if (storeParticipateInActivityRequest == null) {
            throw new BaseException(RetCode.ACTIVITY_NOT_FOUND);
        }
        return storeParticipateInActivityRequest;
    }

    @Override
    public PageInfo<StoreParticipateInActivityRequest> getAllStoreParticipateInActivityRequest(int current, int pageSize) {
        PageHelper.startPage(current, pageSize);
        List<StoreParticipateInActivityRequest> storeParticipateInActivityRequests = storeParticipateInActivityRequestDao.selectAllStoreParticipateInActivityRequest();
        return new PageInfo<StoreParticipateInActivityRequest>(storeParticipateInActivityRequests, 5);
    }

    @Override
    public PageInfo<StoreParticipateInActivityRequest> getAllStoreParticipateInActivityRequestByStoreId(int current, int pageSize, String storeId) {
        PageHelper.startPage(current, pageSize);
        List<StoreParticipateInActivityRequest> storeParticipateInActivityRequests = storeParticipateInActivityRequestDao.selectAllStoreParticipateInActivityRequestByStoreId(storeId);
        return new PageInfo<StoreParticipateInActivityRequest>(storeParticipateInActivityRequests, 5);
    }

    @Override
    @Transactional
    public RetCode processStoreParticipateInActivityRequest(StoreParticipateInActivityRequest storeParticipateInActivityRequest) {
        Date date = new Date();
        storeParticipateInActivityRequest.setProcessTime(date);

        storeParticipateInActivityRequestDao.updateStoreParticipateInActivityRequest(storeParticipateInActivityRequest);

        Integer status = storeParticipateInActivityRequest.getStatus();
        String storeId = storeParticipateInActivityRequest.getStoreId();
        String activityId = storeParticipateInActivityRequest.getActivityId();

        if (status.equals(RequestStatus.APPROVED)) {
            Store store = storeDao.selectStoreByStoreId(storeId);
            store.setActivityId(activityId);
            storeDao.updateStore(store);
        }
        return RetCode.ACTIVITY_SUCCESS;
    }
}
